package com.yiyang.cloud.feign;

import com.yiyang.cloud.pojo.User;
import feign.hystrix.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.math.BigDecimal;

/**
 * className UserFeignClient
 * description UserFeignClient
 *
 * @author Toby
 * @date 2019-7-10
 */
@FeignClient(value = "user-center", fallbackFactory = UserFeignFallbackFactory.class)
public interface UserFeignClient {

    @GetMapping("/users/{id}")
    User findByUserId(@PathVariable("id") Long id);
}

@Component
class UserFeignFallbackFactory implements FallbackFactory<UserFeignClient> {

    @Override
    public UserFeignClient create(Throwable throwable) {
        return new UserFeignClient() {

            @Override
            public User findByUserId(Long id) {
                System.out.println("findByUserIdWithError, userId is: " + id);
                return new User(-1L, "error", "error", 0, BigDecimal.ZERO);
            }
        };
    }
}
